import numpy as np
import matplotlib.pyplot as pyplot
import matplotlib as mpl
mpl.rcParams['font.family'] = 'SimSun'
pyplot.rcParams['axes.unicode_minus'] = False


vail_index = 3
batch_size = 4
pathname = '{}_{}'.format(vail_index, batch_size)

len_windows = 40


for epoch in range(300,301):
    Test_result = []
    Truth = []
    Test_result_True = []


    test_result = np.loadtxt(pathname+'/test_result_epo{}.txt'.format(epoch))
    truth = np.loadtxt(pathname+'/truth_epo{}.txt'.format(epoch))
    test_result_true = test_result - truth
    for i in range(len(test_result)-len_windows+1):
        Test_result.append(np.mean(np.array(test_result[i:i+40])))
        Truth.append(np.mean(np.array(truth[i:i+40])))
        Test_result_True.append(np.mean(np.array(test_result_true[i:i+40])))





    pyplot.plot(Truth, color="blue", alpha=0.5,label='标签')
    pyplot.plot(Test_result, color="red", alpha=0.5,label='实验结果')
    pyplot.plot(Test_result_True, color="green", alpha=0.8,label='实验结果与标签差值')
    # np.savetxt('{}_{}_process/truth_epo{}.txt'.format(vail_index,batch_size,epoch), truth,fmt='%.5f')
    # np.savetxt('{}_{}_process/test_result_epo{}.txt'.format(vail_index,batch_size,epoch), test_result,fmt='%.5f')
    # np.savetxt('{}_{}_process/test_result_truth_epo{}.txt'.format(vail_index,batch_size,epoch), test_result - truth,fmt='%.5f')
    pyplot.grid(True, which='both')
    pyplot.axhline(y=0, color='k')
    # pyplot.ylim((-2, 2))
    pyplot.legend(fontsize=13)
    pyplot.xlabel('样本索引',fontsize=13)
    pyplot.ylabel('剩余寿命占比',fontsize=13)
    pyplot.tight_layout()
    pyplot.savefig('{}_{}_process/epo{}.png'.format(vail_index,batch_size,epoch))
    # pyplot.savefig('{}_{}_process/epo{}.svg'.format(vail_index,batch_size,epoch))
    pyplot.close()


Test_result_True_RMS = (np.sum(np.array(Test_result_True[10000:])**2)/len(Test_result_True[10000:]))**0.5
Test_result_True_MAE = (np.sum(np.abs(np.array(Test_result_True[10000:])))/len(Test_result_True[10000:]))
print(Test_result_True_RMS)